<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pixeldensitymonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_style_pixeldensitymonitor.js.html">pixeldensitymonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utility class that monitors pixel density ratio changes.
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/pixeldensitymonitor.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.labs.style.PixelDensityMonitor&#39;);
<a name="line22"></a>goog.provide(&#39;goog.labs.style.PixelDensityMonitor.Density&#39;);
<a name="line23"></a>goog.provide(&#39;goog.labs.style.PixelDensityMonitor.EventType&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line26"></a>goog.require(&#39;goog.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Monitors the window for changes to the ratio between device and screen
<a name="line33"></a> * pixels, e.g. when the user moves the window from a high density screen to a
<a name="line34"></a> * screen with normal density. Dispatches
<a name="line35"></a> * goog.labs.style.PixelDensityMonitor.EventType.CHANGE events when the density
<a name="line36"></a> * changes between the two predefined values NORMAL and HIGH.
<a name="line37"></a> *
<a name="line38"></a> * This class uses the window.devicePixelRatio value which is supported in
<a name="line39"></a> * WebKit and FF18. If the value does not exist, it will always return a
<a name="line40"></a> * NORMAL density. It requires support for MediaQueryList to detect changes to
<a name="line41"></a> * the devicePixelRatio.
<a name="line42"></a> *
<a name="line43"></a> * @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper which contains the
<a name="line44"></a> *     document associated with the window to listen to. Defaults to the one in
<a name="line45"></a> *     which this code is executing.
<a name="line46"></a> * @constructor
<a name="line47"></a> * @extends {goog.events.EventTarget}
<a name="line48"></a> */
<a name="line49"></a>goog.labs.style.PixelDensityMonitor = function(opt_domHelper) {
<a name="line50"></a>  goog.base(this);
<a name="line51"></a>
<a name="line52"></a>  /**
<a name="line53"></a>   * @type {Window}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.window_ = opt_domHelper ? opt_domHelper.getWindow() : window;
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * The last density that was reported so that changes can be detected.
<a name="line60"></a>   * @type {goog.labs.style.PixelDensityMonitor.Density}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.lastDensity_ = this.getDensity();
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * @type {function (MediaQueryList)}
<a name="line67"></a>   * @private
<a name="line68"></a>   */
<a name="line69"></a>  this.listener_ = goog.bind(this.handleMediaQueryChange_, this);
<a name="line70"></a>
<a name="line71"></a>  /**
<a name="line72"></a>   * The media query list for a query that detects high density, if supported
<a name="line73"></a>   * by the browser. Because matchMedia returns a new object for every call, it
<a name="line74"></a>   * needs to be saved here so the listener can be removed when disposing.
<a name="line75"></a>   * @type {?MediaQueryList}
<a name="line76"></a>   * @private
<a name="line77"></a>   */
<a name="line78"></a>  this.mediaQueryList_ = this.window_.matchMedia ? this.window_.matchMedia(
<a name="line79"></a>      goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_QUERY_) : null;
<a name="line80"></a>};
<a name="line81"></a>goog.inherits(goog.labs.style.PixelDensityMonitor, goog.events.EventTarget);
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * The two different pixel density modes on which the various ratios between
<a name="line86"></a> * physical and device pixels are mapped.
<a name="line87"></a> * @enum {number}
<a name="line88"></a> */
<a name="line89"></a>goog.labs.style.PixelDensityMonitor.Density = {
<a name="line90"></a>  /**
<a name="line91"></a>   * Mode for older portable devices and desktop screens, defined as having a
<a name="line92"></a>   * device pixel ratio of less than 1.5.
<a name="line93"></a>   */
<a name="line94"></a>  NORMAL: 1,
<a name="line95"></a>
<a name="line96"></a>  /**
<a name="line97"></a>   * Mode for newer portable devices with a high resolution screen, defined as
<a name="line98"></a>   * having a device pixel ratio of more than 1.5.
<a name="line99"></a>   */
<a name="line100"></a>  HIGH: 2
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * The events fired by the PixelDensityMonitor.
<a name="line106"></a> * @enum {string}
<a name="line107"></a> */
<a name="line108"></a>goog.labs.style.PixelDensityMonitor.EventType = {
<a name="line109"></a>  /**
<a name="line110"></a>   * Dispatched when density changes between NORMAL and HIGH.
<a name="line111"></a>   */
<a name="line112"></a>  CHANGE: goog.events.getUniqueId(&#39;change&#39;)
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * Minimum ratio between device and screen pixel needed for high density mode.
<a name="line118"></a> * @type {number}
<a name="line119"></a> * @private
<a name="line120"></a> */
<a name="line121"></a>goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_RATIO_ = 1.5;
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Media query that matches for high density.
<a name="line126"></a> * @type {string}
<a name="line127"></a> * @private
<a name="line128"></a> */
<a name="line129"></a>goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_QUERY_ =
<a name="line130"></a>    &#39;(min-resolution: 144dpi), (-webkit-min-device-pixel-ratio: 1.5)&#39;;
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Starts monitoring for changes in pixel density.
<a name="line135"></a> */
<a name="line136"></a>goog.labs.style.PixelDensityMonitor.prototype.start = function() {
<a name="line137"></a>  if (this.mediaQueryList_) {
<a name="line138"></a>    this.mediaQueryList_.addListener(this.listener_);
<a name="line139"></a>  }
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * @return {goog.labs.style.PixelDensityMonitor.Density} The density for the
<a name="line145"></a> *     window.
<a name="line146"></a> */
<a name="line147"></a>goog.labs.style.PixelDensityMonitor.prototype.getDensity = function() {
<a name="line148"></a>  if (this.window_.devicePixelRatio &gt;=
<a name="line149"></a>      goog.labs.style.PixelDensityMonitor.HIGH_DENSITY_RATIO_) {
<a name="line150"></a>    return goog.labs.style.PixelDensityMonitor.Density.HIGH;
<a name="line151"></a>  } else {
<a name="line152"></a>    return goog.labs.style.PixelDensityMonitor.Density.NORMAL;
<a name="line153"></a>  }
<a name="line154"></a>};
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * Handles a change to the media query and checks whether the density has
<a name="line159"></a> * changed since the last call.
<a name="line160"></a> * @param {MediaQueryList} mql The list of changed media queries.
<a name="line161"></a> * @private
<a name="line162"></a> */
<a name="line163"></a>goog.labs.style.PixelDensityMonitor.prototype.handleMediaQueryChange_ =
<a name="line164"></a>    function(mql) {
<a name="line165"></a>  var newDensity = this.getDensity();
<a name="line166"></a>  if (this.lastDensity_ != newDensity) {
<a name="line167"></a>    this.lastDensity_ = newDensity;
<a name="line168"></a>    this.dispatchEvent(goog.labs.style.PixelDensityMonitor.EventType.CHANGE);
<a name="line169"></a>  }
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/** @override */
<a name="line174"></a>goog.labs.style.PixelDensityMonitor.prototype.disposeInternal = function() {
<a name="line175"></a>  if (this.mediaQueryList_) {
<a name="line176"></a>    this.mediaQueryList_.removeListener(this.listener_);
<a name="line177"></a>  }
<a name="line178"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line179"></a>};
</pre>


</body>
</html>
